// 操作数据--获取请求的数据,然后调用 mutations内的方法实现数据的修改
import lodash from "lodash";
import * as types from './mutations-types';
import {ModelDesignApi} from "@/api/modeldesign";
import {bizFields, containers, customFields, sceneFields} from "@c/public/form-designer/widgetsConfig";

//获取模板的配置信息--所谓的节目信息
export const setTemplateInfo = async function ({commit, state}, data) {
    let result = Object();
    try {
        const resp = await ModelDesignApi.getDetail(data);
        if ((resp.code) === 200) {
            let dataInfo = (resp.data);
            let temp = Object();
            Reflect.set(temp, 'id', (dataInfo.id))
            Reflect.set(temp, 'title', (dataInfo.title));
            Reflect.set(temp, 'resolution', (dataInfo.resolution));
            Reflect.set(temp, 'devType', (dataInfo.devType));
            Reflect.set(result, 'state', true);
            Reflect.set(result, 'result', dataInfo);
            let widgetList = JSON.parse(dataInfo.widgetList);
            let comps = lodash.concat(containers, bizFields, customFields, sceneFields);
            widgetList.forEach((item) => {
                let type = (item.type);
                const originalComp = lodash.find(comps, {type: type}) || item;
                for (const key in originalComp['options']) {
                    if (key === 'config') {
                        let keyInfo = originalComp['options'][key];
                        let itemInfo = item['options'][keyInfo];
                        let originalInfo = originalComp['options'][keyInfo];
                        for (const itemInfooKey in itemInfo) {
                            // 处理特殊字段合并
                            if (itemInfooKey === 'colOptions' && lodash.isObject(itemInfo[itemInfooKey])) {
                                let mergeInfo = lodash.unionBy(lodash.cloneDeep(itemInfo[itemInfooKey]), lodash.cloneDeep(originalInfo[itemInfooKey]), 'value');
                                Reflect.set(itemInfo, itemInfooKey, mergeInfo)
                            }
                        }
                    }
                }
                // loadsh _.assign、_.merge 和 _.defaults(对象覆盖)
                Reflect.set(item, 'options', lodash.merge(lodash.cloneDeep(originalComp['options']), lodash.cloneDeep(item['options'])))
            })
            Reflect.set(dataInfo, 'widgetList', widgetList)
            localStorage.setItem('templateInfo', JSON.stringify(temp));
            localStorage.setItem('widget__list__backup', JSON.stringify(widgetList));
            localStorage.setItem('form__config__backup', (dataInfo.formConfig));
            commit(types.SET_TEMPLATE_INFO, dataInfo);
        } else {
            Reflect.set(result, 'state', false);
        }
    } catch (err) {
        Reflect.set(result, 'state', false);
    }
    return result
};

// 存储模板信息
export const setTemplateDataInfo = async function ({commit, state}, data) {
    commit(types.SET_TEMPLATE_INFO, data);
};


// 号别简称的字典
export const setRegisterObjNameOptions = async function ({commit, state}, params) {
    try {
        const {code, data} = await ModelDesignApi.registerObjNameOptions(params);
        if (code === 200) {
            commit(types.SET_REGISTER_OBJ_NAME_OPTIONS_INFO, data);
        }
    } catch (e) {
        // console.log(e)
    }
};


